//
//  ViewController.m
//  NSCoding
//
//  Created by wanglei on 16/12/26.
//  Copyright © 2016年 wanglei. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self archive];
   // [self Solution];
}
// 归档练习
- (void)archive{
    
    Person *person1 = [[Person alloc]init];
    person1.age = 16;
    person1.name = @"吴书敏";
    person1.image = [UIImage imageNamed:@"avatar.jpg"];
    
    // 归档 ， 归档是把 复杂对象转化为一个mutableData 的过程，转化完毕后，把mutaleData 写入到文件
    
    // 1. 创建可变数据
    NSMutableData *data1 = [NSMutableData data];
    
    // 2. 创建一个归档对象 NSKeyedArchiver
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data1];
    
    // 3. 归档
    [archiver encodeObject:person1 forKey:@"p1"];
    
    // 4. 归档完成
    [archiver finishEncoding];
    
    // 5. 把归档完成的数据写入到沙盒
    NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:@"data1"];
    NSLog(@"dataPath = %@",dataPath);
    
    [data1 writeToFile:dataPath atomically:YES];
    
    //self.data = data1;
    
   
    NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data1];
    Person *p2 = [unArchiver decodeObjectForKey:@"p1"];
    [unArchiver finishDecoding];
    NSLog(@"%@", p2);
    NSLog(@"%ld", p2.age);
    NSLog(@"%@", p2.name);
    NSLog(@"%@",p2.image);
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(100, 100, 200, 200))];
    imageView.image = p2.image;
    [self.view addSubview:imageView];
    
}

//解档
- (void)Solution{
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
